use std::path::Path;
use std::fs::File;
use std::io::{self, BufRead};

fn main() {
//    在生成输出之前,hosts文件必须存在当前路径下
    if let Ok(lines) = read_lines("./hosts") {
    //    使用迭代器遍历hosts文件
        for line in lines {
            if let Ok(ip) = line {
                // println!("{}", ip);
                let ip = ip.as_str();
                let mut ip_vec = ip.split(" ");
                let ip = ip_vec.next().unwrap();
                let domain = ip_vec.next().unwrap();
                println!("ip:{}", ip);
                println!("domain:{}", domain);
            }
        }
    }
}
// 输出包裹在Result中以允许匹配错误
// 将迭代器返回给文件行的读取器(BufReader)
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>,{
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}